from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from django.utils.decorators import method_decorator

# Create your views here.

# url(r'^template_view/$', views.TemplateView.as_view())
class TemplateView(View):

    def get(self,request):
        context = {
            "name": "cg",
            "alist": [2,3,4],
            "adict": {"age":"20"}
        }
        return render(request, "index.html", context=context)


def my_decorator(view_func):
    """简单装饰器"""
    def wrapper(request, *args, **kwargs):
        print("我的装饰器生效了")
        return view_func(request, *args, **kwargs)


    return wrapper


# @my_decorator
# def test(request):
#     return HttpResponse("ok")


# 可以只装饰内视图中的get方法
@method_decorator(my_decorator, "get")
class Demoview(View):
    """定义类视图"""
    # @my_decorator
    def get(self, request):
        return HttpResponse("get请求的业务逻辑")

    def post(self, request):
        return HttpResponse("post请求的业务逻辑")


class ListModelMixin(View):
    def list(self, request):
        context = {
            "name_list": "cg",
            "alist_list": [2, 3, 4],
            "adict_list": {"age": "20"}
        }
        return render(request, "index_list.html",context=context)


class CreateModelMixin(View):
    def create(self, request):
        context = {
            "name_create": "cg",
            "alist_create": [2, 3, 4],
            "adict_create": {"age": "20"}
        }
        return render(request, "index_create.html",context=context)


class BookView(ListModelMixin,CreateModelMixin,View):
    def get(self, request):
        list = self.list(request)
        return list


    def post(self, request):
        create = self.create(request)
        return create
